package com.ljl;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Scanner;

/**
 * @author 刘家良
 * @date 2022/10/26 01:11
 * @description
 */
public class ClientMsgHandler extends SimpleChannelInboundHandler<String> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //当连接激活时，会执行该方法
        new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextLine()) {
                String msg = scanner.nextLine();
                System.out.printf("发送消息[%s]\r\n", msg);
                ctx.writeAndFlush(msg);
            }
        }).start();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String s) throws Exception {
        System.out.printf("收到消息[%s]\r\n", s);
    }

}
